<?php
/**
 * @version 1.0
 * @since June-2008
 * @package lib
 * @subpackage data
 * @name CStore
 * @deprecated class base.
 * @todo Lớp trừu tượng mô tả đối tượng lưu trữ. Xử lý các thao tác lưu trữ
*/
class CStore
{
	static $default = false;
	public $relationships = array();
	public $items = array();
	public $connected = false;

	function __construct($items = '')
	{
		if(!$items)
		{
			$this->items = array();
		}
		elseif(is_array($items))
		{
			$this->items = $items;
		}
		elseif(is_string($items))
		{
			eval('$this->items='.$items.';');
		}
		$this->connect();
	}
	function load_schema($file_name)
	{
		$this->relationships = CList(CXml($file_name)->decode())->extract_id('name');
	}
	function connect()
	{
		$this->connected = true;
	}
	function close()
	{
		$this->connected = false;
	}
	function count($table, $conditions=false)
	{
		return is_array($this->items)?sizeof($this->items):0;
	}
	function select($table, $conditions = false, $options = array())
	{
		$result = CList($this->items)->filter($conditions);
		if($result)
		{
			return reset($result);
		}
		return array();
	}
	function select_all($table, $conditions = false, $options = array())
	{
		return CList($this->items)->filter($conditions);
	}
	function fetch($query_string = false)
	{
	}
	function insert($table, $values)
	{
		if(isset($values['id']))
		{
			$this->items[$values['id']] = $values;
		}
		else
		{
			$this->items[] = $values;
		}
	}
	function update($table, $values, $condition)
	{
		$items = $this->select_all($table, $condition);

		foreach($items as $id=>$item)
		{
			if(isset($values['id']) and $values['id']!=$id)
			{
				$this->items[$values['id']] = $this->items[$id];
				unset($this->items[$id]);
				$id = $values['id'];
			}
			foreach($values as $key=>$value)
			{
				$this->items[$id][$key] = $value;
			}
		}
	}
	function delete($table, $conditions)
	{
		foreach($this->items as $id=>&$item)
		{
			if(is_array($conditions))
			{
				if(CRecord($item)->test($conditions))
				{
					unset($this->items[$id]);
				}
			}
			elseif(!$conditions)
			{
				unset($this->items[$id]);
			}
			elseif(is_array($item))
			{
				if(isset($item['id']) and $item['id'] == $conditions)
				{
					unset($this->items[$id]);
				}
			}
			elseif($item == $conditions)
			{
				unset($this->items[$id]);
			}
		}
	}
	function to_string()
	{
		return var_export($this->items,1);
	}
	function condition_string($condition)
	{
	}
	function cascade_update($table, $old_id, $new_id)
	{
		if(isset($this->relationships[$table]))
		{
			foreach($this->relationships[$table]['relationships'] as $relationship)
			{
				foreach($relationship['fields'] as $field)
				{
					$this->update($relationship['table-name'], array($field=>$new_id),array($field=>$old_id));
				}
			}
		}
	}
	function cascade_delete($table, $id)
	{
		if(isset($this->relationships[$table]))
		{
			foreach($this->relationships[$table]['relationships'] as $relationship)
			{
				foreach($relationship['fields'] as $field)
				{
					$items = CTable($relationship['table-name'])->select_all(EEqual($field,EInt($id)));
					$this->delete($relationship['table-name'], array($field=>$id));
					foreach($items as $item)
					{
						$this->cascade_delete($relationship['table-name'], $item['id']);
					}
				}
			}
		}
	}
}
function CStore($items = '')
{
	return new CStore($items);
}
?>